#include<iostream>
#include <netinet/in.h>
#include<sys/socket.h>
#include<string>
#include<cstring>
#include<stdlib.h>
#include <arpa/inet.h>
#include<sys/wait.h>
#include<unistd.h>

void USE(char*ch)
{
    std::cout<<ch<<" ip[]  port[>1024]"<<std::endl;
}
int main(int argc,char*argv[])
{
    if(argc!=3)
    {
        USE(argv[0]);
        exit(0);
    }
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd<0)
    {
        std::cerr<<"sockfd fail"<<std::endl;
    }

    //不需要显示bind；
    //建立联系；
    struct sockaddr_in service;
    memset(&service,0,sizeof(service));
    service.sin_port = htons(std::stoi(argv[2]));
    service.sin_family = AF_INET;
    service.sin_addr.s_addr = inet_addr(argv[1]);
    int n = connect(sockfd,(sockaddr*)&service,sizeof(service));
    if(n<0)
    {
        std::cout<<"connect err"<<std::endl;
        exit(0);
    }
    while(true)
    {
        std::string s;
        std::cout<<"enter @";
        std::getline(std::cin,s);
        int retval = write(sockfd,s.c_str(),s.size());
        char buff[4096];
        retval = read(sockfd,buff,sizeof(buff));
        buff[retval] = '\0';
        std::cout<<buff<<std::endl;
    }
    return 0;
}